/*------------------------------------------------------------------------------
 * PROJ : PrototypeWithSpring
 * NAME : sculove.test.ozrParser.KeicOzrParser.java
 *        
 * Copyright 2009 sculove All rights reserved
 *------------------------------------------------------------------------------
*/
package sculove.test.parser.ozr;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import jxl.CellView;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import sculove.spring.framework.util.SStringUtil;

/**
 * <pre>
 * sculove.test.ozrParser.KeicOzrParser.java
 * TODO
 * 
 * </pre>
 * @date : 2009. 10. 19. ���� 9:30:36
 * @version :
 * @author : sculove
 */
public class KeicOzrParser {
	private final Log logger = LogFactory.getLog(this.getClass());
	
	public Map<String, String> ozrs = new HashMap<String, String>();
	
	public static void main(String[] args) {
		KeicOzrParser ozrParser = new KeicOzrParser();
		File baseDir = new File("E:/oz");
		
		System.out.println("--Start to parse Ozr...");
        if(baseDir.isDirectory()) {
        	ozrParser.searchOzr(baseDir);
        }
        System.out.println("--End to parse Ozr...");
        System.out.println("--Start to write excel...");
        ozrParser.writeExcel("C:/OZR���.xls");
        System.out.println("--End to write excel...");
	}
	
    public void searchOzr(File target) {
        if(target.isDirectory()) {    // Directory�� ���
            String[] child = target.list();
            for(int i = 0; i < child.length; i++){
            	searchOzr(new File( target.getAbsolutePath() + "\\" + child[i]));
            }
        } else {    // File�� ���
            if("ozr".equalsIgnoreCase(SStringUtil.unqualify(target.getAbsolutePath(), '.')) 
            		&& (SStringUtil.unqualify(target.getAbsolutePath(), '\\').length() == 14)
            ) {
            	logger.info("�ڣ� " + target.getAbsolutePath());
            	String type = parsingFile(target);
            	ozrs.put(target.getAbsolutePath(), type);
            }
        }
    }
    
    /**
     * 
     * @return
     * @throws Exception
     */
    public String parsingFile(File file) {
    	BufferedReader br = null;
    	String type = "���?";
    	
    	try {
        	br = new BufferedReader(new FileReader(file));
        	
        	String lineContent;
        	while ( (lineContent = br.readLine()) != null ) {
        		if( lineContent.indexOf("ZZZZ050R05") != -1) {			// ����
        			type = "�����ȹ�";
        			logger.info("�� type : " + type);
        			break;
        		} else if( lineContent.indexOf("ZZZZ050R05") != -1) {	// �뺸��
        			type = "�뺸��";
        			logger.info("�� type : " + type);
        			break;
        		} 
        	}		
		} catch (Exception e) {
		} finally {
			try { if(br != null) { br.close(); br = null; } } catch(Exception be) {}
		}
    	return type;
    }    
    
    /**
     * Excel �����
     * @param filepath
     */
    private void writeExcel(String filepath) {
        WritableWorkbook workbook = null;
        WritableSheet sheet = null;
        File excelFile = new File(filepath);
        
        try{
            workbook = Workbook.createWorkbook(excelFile);
            sheet = workbook.createSheet("���? ���", 0);
            
            // �÷� Auto size ����
            CellView autoSizeCellView = new CellView();
            autoSizeCellView.setAutosize(true);
            sheet.setColumnView(0, autoSizeCellView);
            sheet.setColumnView(1, autoSizeCellView);
            sheet.setColumnView(2, autoSizeCellView);
            sheet.setColumnView(3, autoSizeCellView);
            
            sheet.addCell(new Label(0,0,"������"));
            sheet.addCell(new Label(1,0,"�����ڵ�"));
            sheet.addCell(new Label(2,0,"����"));
            sheet.addCell(new Label(3,0,"���?����"));
            
            int row = 1;
            Iterator<String> iter = ozrs.keySet().iterator();
            while(iter.hasNext()) {
                String key = iter.next();
                String appCd = key.substring(key.indexOf("oz\\")+3,key.indexOf("oz\\")+6);
                String appNm ="";
                if(appCd.equals("Keic")) appNm="����"; 
                else if(appCd.equals("itc")) appNm="ITSM";
                else if(appCd.equals("mac")) appNm="ȸ��";
                else if(appCd.equals("mhc")) appNm="�λ�";
                else if(appCd.equals("muc")) appNm="��������";
                else if(appCd.equals("rac")) appNm="�μ�";
                else if(appCd.equals("rcc")) appNm="����";
                else if(appCd.equals("rec")) appNm="���̹����⺸����";
                else if(appCd.equals("ric")) appNm="�ſ�����";
                else if(appCd.equals("rmc")) appNm="��ǰ";
                else if(appCd.equals("rrc")) appNm="���İ�";
                else if(appCd.equals("rsc")) appNm="�?����";
                else if(appCd.equals("ruc")) appNm="��������";
                else if(appCd.equals("zzz")) appNm="�������";
            	
            	sheet.addCell(new Label(0,row, appNm));
                sheet.addCell(new Label(1,row, appCd));
                sheet.addCell(new Label(2,row, ozrs.get(key)));
                sheet.addCell(new Label(3,row, key.substring(key.indexOf("oz\\")+3)));
                row++;
            }
        }catch(Exception e){
           e.printStackTrace();
        } finally {
            try{
                if(workbook != null) workbook.write();
                if(workbook != null) workbook.close();   
            }catch(Exception e2){}
        }
    }       
}
